package hk.m4s.lr.repair.test.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.DeviceRepalyModel;
import hk.m4s.lr.repair.test.ui.equipment.DeviceReplayActivity;

/**
 * Created by qi.wang on 2017/3/27.
 */

public class DeviceReplayAdapter extends BaseAdapter {
    List<DeviceRepalyModel> list = new ArrayList<>();
    DeviceReplayActivity context;
    LayoutInflater inflater;

    public DeviceReplayAdapter(DeviceReplayActivity context, List<DeviceRepalyModel> data) {
        this.context = context;
        this.list = data;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_item_device_replay, parent, false);
            holder.device_title = (TextView) convertView.findViewById(R.id.device_title);
            holder.device_money = (TextView) convertView.findViewById(R.id.device_money);
            holder.device_contct = (TextView) convertView.findViewById(R.id.device_contct);
            holder.device_phone=(TextView) convertView.findViewById(R.id.device_phone);
            holder.device_address=(TextView) convertView.findViewById(R.id.device_address);
            holder.device_hulv=(Button) convertView.findViewById(R.id.device_hulv);
            holder.device_ok=(Button) convertView.findViewById(R.id.device_ok);
            holder.device_info=(TextView) convertView.findViewById(R.id.device_info);
            holder.callPhone=(LinearLayout) convertView.findViewById(R.id.callPhone);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.device_ok.setTag(position);
        holder.device_hulv.setTag(position);
        holder.callPhone.setTag(position);
        DeviceRepalyModel businessModel=list.get(position);

        try {
            holder.device_title.setText(businessModel.getName());
            holder.device_contct.setText(businessModel.getContact());
            holder.device_money.setText(businessModel.getServicePrice());
            holder.device_phone.setText(businessModel.getContactPhone());
            holder.device_address.setText(businessModel.getAddressInfo());
            if(businessModel.getState().equals("1")){
                holder.device_hulv.setVisibility(View.VISIBLE);
                holder.device_ok.setVisibility(View.VISIBLE);
                holder.device_info.setVisibility(View.GONE);
            }else{
                holder.device_hulv.setVisibility(View.GONE);
                holder.device_ok.setVisibility(View.GONE);
                holder.device_info.setVisibility(View.VISIBLE);
                if(businessModel.getState().equals("-1")){
                    holder.device_info.setText("已忽略");
                }else if(businessModel.getState().equals("2")){
                    holder.device_info.setText("已完成");
                }else if(businessModel.getState().equals("3")){
                    holder.device_hulv.setVisibility(View.VISIBLE);
                    holder.device_ok.setVisibility(View.VISIBLE);
                    holder.device_info.setVisibility(View.GONE);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        holder.device_hulv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num=(Integer)v.getTag();
                DeviceRepalyModel businessModel=list.get(num);
                context.showTips("1",businessModel.getId(),"确定要忽略吗?");
            }
        });
        holder.device_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num=(Integer)v.getTag();
                DeviceRepalyModel businessModel=list.get(num);
                context.showTips("2",businessModel.getId(),"对方报价"+businessModel.getServicePrice()+",元确定执行吗?");
            }
        });
        holder.callPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num=(Integer)v.getTag();
                DeviceRepalyModel businessModel=list.get(num);
                context.callPhone(businessModel.getContactPhone());
            }
        });
        return convertView;
    }

    public static class ViewHolder {
        TextView device_title;
        TextView device_money;
        TextView device_contct;
        TextView device_phone;
        TextView device_address;
        TextView device_info;
        Button device_hulv,device_ok;
        LinearLayout callPhone;
    }
}
